﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;
using System.IO;
using System.Data.Odbc;
namespace GUI
{
    public partial class FormNhapXuatTon : Window
    {
        public FormNhapXuatTon()
        {
            InitializeComponent();
            hoiThoat = false;
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void laydulieuNXT()
        {
           
                DateTime st = dtbBatDau.Value;
                DateTime en = dtbKetThuc.Value;
                System.Data.OleDb.OleDbDataAdapter da = BUS.SanPhamBUS.BCNXT(st, en);
                System.Data.DataSet ds = new DataSet();
                da.Fill(ds);
                dataGridViewNXT.Columns.Clear();
                dataGridViewNXT.DataSource = ds.Tables[0];
                dataGridViewNXT.Columns[0].HeaderText = "Mã SP";
                dataGridViewNXT.Columns[1].HeaderText = "Tên SP";
                DataGridViewTextBoxColumn Sodudauthang = new DataGridViewTextBoxColumn();
                dataGridViewNXT.Columns.Add(Sodudauthang);
                dataGridViewNXT.Columns[2].HeaderText = "Số dư đầu tháng";
                DataGridViewTextBoxColumn sodu = new DataGridViewTextBoxColumn();
                dataGridViewNXT.Columns.Add(sodu);
                string ngay1 = st.ToString("dd/MM/yyyy");
                dataGridViewNXT.Columns[3].HeaderText = "Số dư " + ngay1;
                DataGridViewTextBoxColumn TongNhap = new DataGridViewTextBoxColumn();
                dataGridViewNXT.Columns.Add(TongNhap);
                dataGridViewNXT.Columns[4].HeaderText = "Tổng nhập";
                DataGridViewTextBoxColumn TongXuat = new DataGridViewTextBoxColumn();
                dataGridViewNXT.Columns.Add(TongXuat);
                dataGridViewNXT.Columns[5].HeaderText = "Tổng Xuất";
                DataGridViewTextBoxColumn TonKho = new DataGridViewTextBoxColumn();
                dataGridViewNXT.Columns.Add(TonKho);
                dataGridViewNXT.Columns[6].HeaderText = "Tồn Kho";
                for (int i = 0; i <= dataGridViewNXT.RowCount-1; i++)
                {
                    DateTime s = new DateTime(st.Year, st.Month, 1);
                    int masp = int.Parse(dataGridViewNXT[0, i].Value.ToString());
                    SanPhamBUS temp = new SanPhamBUS();
                    int tondk = temp.SoDuDK(masp, s);
                    int tondaungay = tondk + temp.TongNhap(masp, s, st) - temp.TongXuat(masp, s, st);
                    int tongxuat = temp.TongXuat(masp, st, en);
                    int tongnhap = temp.TongNhap(masp, st, en);
                    int tonkho = tondaungay + tongnhap - tongxuat;
                    dataGridViewNXT[2, i].Value = tondk;
                    dataGridViewNXT[3, i].Value = tondaungay;
                    dataGridViewNXT[4, i].Value = tongnhap;
                    dataGridViewNXT[5, i].Value = tongxuat;
                    dataGridViewNXT[6, i].Value = tonkho;
                }
            
           
        }
        private void FormNhapXuatTon_Load(object sender, EventArgs e)
        {
            laydulieuNXT();
        }

        private void btnXem_Click(object sender, EventArgs e)
        {
            laydulieuNXT();
        }


        private void btnXuat_Click(object sender, EventArgs e)
        {
            XuatExcel(dataGridViewNXT, "NXT");
        }

    }
}
